﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;

namespace CourseSystem.Web.Controllers
{
    public class CommentController : BaseController
    {
        //
        // GET: /Comment/

        public ActionResult Index(int? type)
        {
            type = type.CheckInt(0);
            ViewData["type"] = type;
            if (type == 0)
            {
                return View();
            }
            if (type == 1)
            {
                return RedirectToAction("Expert");
            }
            return NotFound();
        }

        public ActionResult Expert()
        {
            ViewData["type"] = 1;
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="commentContent"></param>
        /// <param name="type">0教师1课程</param>
        /// <param name="rankHid"></param>
        /// <param name="reviewType"></param>
        /// <param name="teacherId"></param>
        /// <param name="reviewerName"></param>
        /// <returns></returns>
        public ActionResult CommentEdit(string commentContent, int type, int rankHid, int reviewType, int? teacherId, string reviewerName)
        {
            var actionRed = reviewType == 0 ? "Index" : "Expert";
            teacherId = teacherId.CheckInt(0);
            if (string.IsNullOrEmpty(commentContent))
            {
                SetAlertMsg("请填写评价内容!", true);
                return RedirectToAction(actionRed);
            }
            var now = DateTime.Now;
            if (reviewType == 0 && UserHelper.LoginUser.UserType != UserTypeEnum.Student)
            {
                SetAlertMsg("登录用户非学生身份!", true);
                return RedirectToAction(actionRed);
            }
            if (type == 0 && teacherId.Value == 0)
            {
                SetAlertMsg("请选择评价教师!", true);
                return RedirectToAction(actionRed);
            }
            var add = new CourseCommentInfo { Comment = commentContent, CommentTime = now, CommentType = type, Rank = rankHid };
            if (type == 0)
            {
                add.TeacherId = teacherId.Value;
                var teaInfo = TeacherBLL.Get(teacherId.Value);
                if (teaInfo == null)
                {
                    SetAlertMsg("请选择评价教师!", true);
                    return RedirectToAction(actionRed);
                }
                add.TeacherName = teaInfo.Name;
            }

            if (reviewType == 0)
            {
                add.ReviewerId = UserHelper.LoginUser.Id;
                add.ReviewerType = 0;
                add.ReviewerName = UserHelper.LoginUser.UserName;
            }
            else
            {
                add.ReviewerType = 1;
                add.ReviewerName = reviewerName ?? "专家";
            }
            if (CourseCommentBLL.Add(add))
            {
                SetAlertMsg("评价成功!", true);
                return RedirectToAction(actionRed);
            }
            SetAlertMsg("未知错误!", true);
            return RedirectToAction(actionRed);
        }
    }
}
